import {getLoadingObject, showElMessage} from "./common";
import axios from "axios";

export async function postRequest(url, requestData, successCallback, failCallback, errorCallback=null, showTip=true) {
    let loading = getLoadingObject();

    try {
        const response = await axios.post(url, requestData);
        const data = response.data;

        if (data.state === -1) {
            if (showTip) {
                showElMessage(false, data.message, "warning");
            }
            if (failCallback) {
                failCallback(data);
            }
        } else {
            if (showTip) {
                showElMessage(false, data.message, "success");
            }
            if (successCallback) {
                successCallback(data);
            }
        }
    } catch (error) {
        console.log(error);
        showElMessage(false, error, "error");
        if (errorCallback) {
            errorCallback(error);
        }
    } finally {
        loading.close();
    }
}
